package cn.com.sueframe.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.dispatcher.FilterDispatcher;
/**
 * Struts2核心处理器
 * @author anmin
 * Create 2013-3-22
 */
public class Struts2Controller extends FilterDispatcher {
	//缓存时长
    private String cacheTime = "0";
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest)request;
		StringBuffer urlB = httpRequest.getRequestURL();
		String url = urlB.toString();
		HttpServletResponse httpResponse = (HttpServletResponse)response;
		if(url.contains("ceswich.action")){
			//假如拦截到得请求时转换语言请求，则清空缓存
			httpResponse.setHeader("Cache-Control", "no-cache");
			httpResponse.setDateHeader("Expires", 0);
			httpResponse.setHeader("Pragma", "No-cache");
		}else{
			//根据具体参数配置设置缓存时间
			 httpResponse.setHeader("Cache-Control", "private,max-age=" + cacheTime);
		}
		super.doFilter(httpRequest, httpResponse, chain);
	}
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		
		cacheTime = filterConfig.getInitParameter("cacheTime");
		cacheTime = cacheTime == null || "".equals(cacheTime) ? "0" : cacheTime;
		super.init(filterConfig);
	}
	
        
}
